package org.dtrd.modules.faq.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.faq.entity.dto.FaqCategoryDetail;
import org.dtrd.modules.faq.entity.po.DtrdExtEmsYwFaqCategory;
import org.dtrd.modules.faq.mapper.DtrdExtEmsYwFaqCategoryMapper;
import org.dtrd.modules.faq.service.IDtrdExtEmsYwFaqCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 知识问答分类 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-06
 */
@Service
@DS("multi-datasource1")
public class DtrdExtEmsYwFaqCategoryServiceImpl extends ServiceImpl<DtrdExtEmsYwFaqCategoryMapper, DtrdExtEmsYwFaqCategory> implements IDtrdExtEmsYwFaqCategoryService {

    private DtrdExtEmsYwFaqCategoryMapper dtrdExtEmsYwFaqCategoryMapper;

    @Override
    public List<FaqCategoryDetail> getFaqCategoryList(String fagCategoryName) {
        LambdaQueryWrapper<DtrdExtEmsYwFaqCategory> queryWrapper = baseQueryWrapper();
        if (fagCategoryName != null) {
            queryWrapper.eq(DtrdExtEmsYwFaqCategory::getCategoryName, fagCategoryName);
        }
        List<DtrdExtEmsYwFaqCategory> poList = list(queryWrapper);
        List<FaqCategoryDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new FaqCategoryDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public FaqCategoryDetail getFaqCategory(Integer dataId) {
        DtrdExtEmsYwFaqCategory dtrdExtEmsYwFaqCategory = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwFaqCategory)
                        .map(po -> new FaqCategoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateFaqCategory(FaqCategoryDetail dto) {
        DtrdExtEmsYwFaqCategory dtrdExtEmsYwFaqCategory = new DtrdExtEmsYwFaqCategory();
        if(dto.getDataId() == null){
            dtrdExtEmsYwFaqCategory = dtrdExtEmsYwFaqCategory.init();
        }
        dtrdExtEmsYwFaqCategory.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwFaqCategory);
    }

    @Override
    public boolean removeFaqCategory(Integer dataId) {
        return removeById(dataId);
    }

    public LambdaQueryWrapper<DtrdExtEmsYwFaqCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwFaqCategory.class)
                        .eq(DtrdExtEmsYwFaqCategory::getIsDel, 0);
    }

    public LambdaUpdateWrapper<DtrdExtEmsYwFaqCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwFaqCategory.class)
                        .eq(DtrdExtEmsYwFaqCategory::getIsDel, 0);
    }
}
